package net.portly.cst;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.portly.cst.flex.FlexItemType;
import net.portly.cst.loader.ATDLoader;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class CSTFixture {

	public static final String ID_1001 = "1001";
	public static final String ID_1002 = "1002";
	
	public static final String FWUID_ABC = "ABC";
	public static final String FWUID_DEF = "DEF";

	public AssetTypeDescriptor createATD(String resource) {
		Document doc = loadDocument(resource);
		ATDLoader xp = new ATDLoader();
		xp.loadAttributes(new CSTDocument(doc, "DUMMY"));
		AssetTypeDescriptor atd = xp.getATD();
		return atd;
	}
	
	private Document loadDocument(String string) {
		InputStream docStream = this.getClass().getResourceAsStream(string);
		
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		Document doc = null;
		try {
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			doc = dBuilder.parse(docStream);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return doc;
	}


	public static Map<String, String> createMetaMap(String id, String fw_uid) {
		Map<String, String> metaMap_1 = new HashMap<String, String>();
		metaMap_1.put("id", id);
		metaMap_1.put("fw_uid", fw_uid);
		return metaMap_1;
	}

	public static AssetTypeDescriptor createAssetTypeDescriptor(FlexItemType flexdef, String id, String fw_uid) {
		return createAssetTypeDescriptor(flexdef, id, fw_uid, null);
	}

	public static AssetTypeDescriptor createAssetTypeDescriptor(FlexItemType flexdef, String id, String fw_uid, String assetType) {
		Map<String, String> metaDataMap = new HashMap<String, String>();
		metaDataMap.put("id", id);
		metaDataMap.put("fw_uid", fw_uid);
		metaDataMap.put("assettype", assetType);
		metaDataMap.put("complexAssetClass", flexdef.getComplexAssetClass());
		return new AssetTypeDescriptor(metaDataMap);
	}


}
